package com.my.web.mudules.login;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

import com.my.core.annotation.shiro.RouterMapping;
import com.my.jfinal.plugin.shiro.ShiroKit;
import com.my.web.common.CommonController;


@RouterMapping(url = "/login", viewPath = "/WEB-INF")
public class LoginController extends CommonController{

	public void index() {
		render("modules/login/index.jsp");
	}
	
	public void login() {
		try {
			Subject subject = SecurityUtils.getSubject();
			UsernamePasswordToken token = new UsernamePasswordToken(
					getPara("username"), getPara("password"),
					getPara("rememberMe"));
			subject.login(token);
			redirect(ShiroKit.getSuccessUrl());
		}catch(AuthenticationException e){
			renderText("用户名或密码错误");
		}
	}
	
	public void logout() {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		redirect(ShiroKit.getLoginUrl());
	}
}
